package de.riceman.simpleGo.commands;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.richclient.command.ActionCommand;

import de.riceman.simpleGo.main.GoGame;

/**
 * 
 * 
 * @author Mario Reissmann
 *
 */
public class ContinueCommand extends ActionCommand implements ApplicationContextAware {

	private final Log logger = LogFactory.getLog(getClass());
	
	ApplicationContext applicationContext;
	
	
	@Override
	protected void doExecuteCommand() {

		if( logger.isInfoEnabled() ) {
            logger.info("Continuation requested.");
        }
		
		new Thread(
	            new Runnable() {
	                public void run() {
	                    try {
	                    	GoGame goGame = (GoGame) applicationContext.getBean("GoGame");
	                    	goGame.start();                       
	                    } catch (Exception e) {
	                        e.printStackTrace();
	                    }
	                }
	            }).start();
		
		//It doesn't work via ApplicationEvent, dunno why :-(
		//applicationContext.publishEvent(
			//	new SimpleGoLifecycleApplicationEvent(SimpleGoLifecycleApplicationEvent.CONTINUE, this));

	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
		
		if( logger.isDebugEnabled() ) {
            logger.debug("ApplicationContext set for ContinueCommand.");
        }
	}
}
